//
//  BGOAuthProgressView.swift
//  WeiboSwift
//
//  Created by 战场原黑仪 on 16/7/22.
//  Copyright © 2016年 战场原黑仪. All rights reserved.
//

import UIKit

class BGOAuthProgressView: UIProgressView {

    //设置计时器
    private var timer: NSTimer?
    //设置是否完成
    var isAnimation: Bool = false
    
    override init(frame: CGRect) {
        //自定义SIZE
        super.init(frame: CGRect(x: 0, y: 64, width: SCREEN_W, height: 2))
        self.tintColor = RandomColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //一些对外开放的方法
    func startAnim() {
        self.progress = 0
        self.hidden = false
        
        //设置计时器
        timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: #selector(changeValue), userInfo: nil, repeats: true)
        
    }
    
    func endAnim() {
        self.isAnimation = false
    }
    
    //MARK: - 一些不对外调用的方法
    @objc private func changeValue() {
        if isAnimation {
            
            self.progress += 0.05
            
            if self.progress >= 0.95 {
                self.progress = 0.95
            }
        }else {
            
            
            if self.progress == 1{
                self.hidden = true
                self.timer?.invalidate()
            }else {
                self.progress += 0.05
            }

        }
    }
    
    
    //销毁时
    deinit {
        timer?.invalidate()
    }
    
}
